package com.qsl.ssyx.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsl.ssyx.sys.service.RegionService;
import com.qsl.ssyx.common.result.Result;
import com.qsl.ssyx.model.sys.Region;
import com.qsl.ssyx.vo.sys.RegionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 地区表 前端控制器
 * </p>
 *
 * @author 青衫泪
 * @since 2023-08-08
 */
@Api(tags = "地区接口")
@RestController
@RequestMapping("/admin/sys/region")
public class RegionController {

    @Autowired
    private RegionService regionService;


    @ApiOperation("根据地区名称查询")
    @GetMapping("findRegionByKeyword/{keyword}")
    public Result findRegionByKeyword(@ApiParam(value = "关键字") @PathVariable String keyword) {
        List<Region> region = regionService.findRegionByKeyword(keyword);
        return Result.ok(region);
    }

    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result selectPage(@ApiParam(value = "当前页") @PathVariable Integer page
            , @ApiParam(value = "每页记录数") @PathVariable Integer limit
            , @ApiParam(value = "关键字") RegionVo regionVo) {
        Page<Region> pageParam = new Page<>(page, limit);
        IPage<Region> model = regionService.selectPage(pageParam, regionVo);
        return Result.ok(model);
    }

    @ApiOperation("新增")
    @PostMapping("save")
    public Result save(@RequestBody Region region) {
        boolean flag = regionService.save(region);

        return Result.ok(flag);
    }

    @ApiOperation("根据Id查询")
    @PostMapping("getById/{Id}")
    public Result save(@ApiParam(value = "Id") @PathVariable Integer Id) {
        Region region = regionService.getById(Id);
        return Result.ok(region);
    }

    @ApiOperation("修改")
    @PutMapping("update")
    public Result update(@RequestBody Region region) {
        boolean flag = regionService.updateById(region);
        return Result.ok(flag);
    }

    @ApiOperation("删除")
    @DeleteMapping("remove/{Id}")
    public Result remove(@PathVariable Integer Id) {
        boolean flag = regionService.removeById(Id);
        return Result.ok(flag);
    }

}

